package com.jeff.utils.security;

import java.io.InputStream;
import java.security.MessageDigest;

/**
 * author：duff
 * version：1.0.0
 * date：2017/8/31
 * desc：
 */
public class MD5Util {
    private static final String ALGORITHM = "MD5";

    /**
     * 对字符串进行md5加密处理
     *
     * @param string 原始字符串
     * @return 加密后的字符串 16位
     */
    public static String get16MD5String(String string) {
        byte[] md5 = stringToMD5(string);
        return to16HexString(md5);
    }

    /**
     * 对字符串进行md5加密处理
     *
     * @param string 原始字符串
     * @return 加密后的字符串 32位
     */
    public static String get32MD5String(String string) {
        byte[] md5 = stringToMD5(string);
        return to32HexString(md5);
    }

    private static byte[] stringToMD5(String string) {
        if (string == null) {
            return null;
        }

        try {
            MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
            digest.update(string.getBytes());
            return digest.digest();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static String to16HexString(byte[] md5) {
        if (md5 == null) {
            return null;
        }

        int beginConvertIndex = 8;
        int endConvertIndex = 24;

        return to32HexString(md5).substring(beginConvertIndex, endConvertIndex);
    }

    public static String to32HexString(InputStream inputStream) {
        byte[] buffer = new byte[1024];
        int len;
        try {
            MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
            while ((len = inputStream.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            return to32HexString(digest.digest());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static String to32HexString(byte[] md5) {
        if (md5 == null) {
            return null;
        }

        int val;
        StringBuilder stringBuffer = new StringBuilder("");
        for (byte b : md5) {
            val = b;
            if (val < 0) {
                val += SecurityUtil.ROUND_8BITS;
            }
            if (val < SecurityUtil.ROUND_4BITS) {
                stringBuffer.append("0");
            }
            stringBuffer.append(Integer.toHexString(val));
        }

        return stringBuffer.toString();
    }

}
